package com.edu.zju.scs.panacea.controller.resource;

import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.resource.Site;
import com.edu.zju.scs.panacea.service.resource.SiteService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@EnableAutoConfiguration
public class SiteController {
    @Autowired
    private SiteService siteService;

    @ResponseBody
    @ApiOperation(value="所有站点查询", notes="所有站点查询")
    @RequestMapping(value = "/site/all", method = RequestMethod.GET)
    public BaseResult getAllSite() {
        return siteService.getAllSite();
    }

    @ResponseBody
    @ApiOperation(value="根据id查询某个站点", notes="根据id查询某个站点")
    @RequestMapping(value = "/site/{id}", method = RequestMethod.GET)
    public BaseResult getSite(@PathVariable Long id) {
        return siteService.getSite(id);
    }

    @ResponseBody
    @ApiOperation(value="根据ID删除站点", notes="根据ID删除站点")
    @RequestMapping(value = "/site/deletion/{id}", method = RequestMethod.GET)
    public BaseResult deleteSite(@PathVariable Long id) {
        return siteService.deleteSite(id);
    }

    @ResponseBody
    @ApiOperation(value="新增站点", notes="新增站点")
    @RequestMapping(value = "/site/new", method = RequestMethod.POST)
    public BaseResult addSite(
            @ApiParam("自有ID") @RequestParam(value = "ownId") String ownId,
            @ApiParam("站点名称") @RequestParam(value = "name") String name,
            @ApiParam("站点类型") @RequestParam(value = "type") Integer type) {
        Site site = new Site(ownId, name, type);
        return siteService.addSite(site);
    }

    @ResponseBody
    @ApiOperation(value="修改站点信息", notes="修改站点信息")
    @RequestMapping(value = "/site/edit", method = RequestMethod.POST)
    public BaseResult editSite(
            @ApiParam("ID") @RequestParam(value = "id") Long id,
            @ApiParam("自有ID") @RequestParam(value = "ownId") String ownId,
            @ApiParam("站点名称") @RequestParam(value = "name") String name,
            @ApiParam("站点类型") @RequestParam(value = "type") Integer type) {
        Site site = new Site(id, ownId, name, type);
        return siteService.editSite(site);
    }

    @ResponseBody
    @ApiOperation(value="站点登录", notes="站点登录")
    @RequestMapping(value = "/site/login", method = RequestMethod.GET)
    public BaseResult agentALogin(
            @ApiParam("用户名") @RequestParam(value = "name") String name,
            @ApiParam("密码") @RequestParam(value = "password") String password,
            HttpSession session) {
        return siteService.isValidSite(name, password, session);
    }
}
